package com.nf.health.app.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

import com.nf.health.app.R;
import com.nf.health.app.models.SleepDelayModel;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.LogUtil;

import java.util.List;

import cn.bluetools.SleepsData;

public class ChartView extends View {

	//private List<SleepsData> xIndex;
	private List<SleepDelayModel> xIndex;
	private Paint paint;
	private int height;
	private int wakeColor;
	private int startColor;
	private int lightColor;
	private int deepColor;
	private int tipHeight;
	private Paint textPaint;
	public ChartView(Context context,List<SleepDelayModel> xIndex) {
		super(context);
		this.xIndex = xIndex;
		height = DensityUtil.dip2px(getContext(), 180);
		tipHeight = DensityUtil.dip2px(getContext(), 15);
		paint = new Paint();
		wakeColor = context.getResources().getColor(R.color.red);
		startColor = context.getResources().getColor(R.color.green);
		lightColor = context.getResources().getColor(R.color.blue1);
		deepColor = context.getResources().getColor(R.color.purple);
		textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setColor(context.getResources().getColor(R.color.black2));
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTextSize(18);
	}

	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		/*setMeasuredDimension(200, height);*/
		int widthTotle = 0;
		int heightTotle = height;
		for (int i = 0; i < xIndex.size() - 1; i++) {
			widthTotle += xIndex.get(i).getDelay();
		}
		setMeasuredDimension(widthTotle, heightTotle + tipHeight);
		//LogUtil.e("111",widthTotle +"-----" + heightTotle+"------" +widthMeasureSpec + "--------------" + heightMeasureSpec);
	}
	
	/*@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int x = 0;
		for(int i = 0;i<xIndex.size()-1;i++){
			if(i==0){
				paint.setColor(wakeColor);
			}else if(xIndex.get(i).status == 0){
				paint.setColor(deepColor);
			}else if(xIndex.get(i).status == 1){
				paint.setColor(lightColor);
			}else{
				paint.setColor(wakeColor);
			}
//			canvas.drawLine(xIndex.get(i).time_stamp, 0, xIndex.get(i+1).time_stamp, height, paint);
//			canvas.drawRect(xIndex.get(i).time_stamp, 0, xIndex.get(i+1).time_stamp, height, paint);
//			x = xIndex.get(i).time_stamp/60;
			canvas.drawRect(xIndex.get(i).time_stamp, 0, xIndex.get(i+1).time_stamp, height, paint);
		}
	}*/
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int left = 0;
		int top = tipHeight;
		int right = 0;
		int bottom = height;
		for (int i = 0; i < xIndex.size() - 1; i++) {
			if ((xIndex.get(i).getStatus() == 1)) {
				paint.setColor(deepColor);
			} else if (xIndex.get(i).getStatus() == 2) {
				paint.setColor(lightColor);
			} else if (xIndex.get(i).getStatus() == 3) {
				paint.setColor(wakeColor);
			} else if (xIndex.get(i).getStatus() == 4) {
				paint.setColor(startColor);
			}
			if(i == 0 ){
				left = 0 ;
			}else{
				left = xIndex.get(i -1).getDelay() +left ;
				if( (i % 3 == 0 && i != (xIndex.size() -2)) || i == (xIndex.size() -3)){  //绘制文字
					canvas.drawText(DateUtil.date2String(xIndex.get(i).getCurrentLongTimre() * 1000, "HH:mm"), left, tipHeight /4 *3 , textPaint);
				}
			}
			right = xIndex.get(i).getDelay() + right;
			canvas.drawRect(left, top, right, bottom, paint);
			//LogUtil.e("222","i="+i+":    "+ xIndex.get(i).getStatus() + "--------" + xIndex.get(i).getDelay()+" ------- " +DateUtil.date2String(xIndex.get(i).getCurrentLongTimre() * 1000, "HH:mm"));
		}
	}
}
